package com.artqiyi.dollmachine.common.Base;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.Toast;

import com.artqiyi.dollmachine.R;
import com.artqiyi.dollmachine.presenter.base.BasePresenter;
import com.artqiyi.dollmachine.presenter.base.BasePresenterCallback;
import com.artqiyi.dollmachine.utils.manager.ActivityManager;
import com.artqiyi.dollmachine.views.widget.MultipleStatusView;
import com.artqiyi.dollmachine.views.widget.ToolBarView;
import com.artqiyi.dollmachine.views.widget.dialog.DialogMaker;
import com.artqiyi.dollmachine.views.widget.dialog.LoadingDialog;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;


/**
 * Activity基类，带Toolbar和MultipleStatusView
 */
public abstract class BaseActivity<P extends BasePresenter> extends AppCompatActivity implements IBaseView
        , MultipleStatusView.OnRetryClickListener, BasePresenterCallback {

    protected ToolBarView mToolBar;
    protected MultipleStatusView mMultipleStatusView;

    LoadingDialog loadingDialog;

    //定义Presenter
    protected P mPresenter;
//    protected CompositeSubscription mCompositeSubscription;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= 21) {
            getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.c7373ff));
        }
        if (setEventBusEnable()) {
            EventBus.getDefault().register(this);
        }
        ActivityManager.getAppManager().addActivity(this);

        mPresenter = getPresenter();
        if (mPresenter != null) {
            mPresenter.setView(this);
            mPresenter.setContext(this);
            mPresenter.setCallback(this);
        }
//        mCompositeSubscription = new CompositeSubscription();
        if (null != getIntent()) {
            handleIntent(getIntent());
        }
        initLayoutRes();
        ButterKnife.bind(this);
        initView(savedInstanceState);
        loadData();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (null != intent) {
            handleIntent(intent);
        }
    }

    protected void handleIntent(Intent intent) {
    }

    @Override
    protected void onResume() {
        super.onResume();
        reLoadData();
    }

    private void initLayoutRes() {
        if (setUseLayoutIdCustom()) {
            setContentView(getLayoutId());
        } else {
            setContentView(R.layout.layout_base_view);
            mToolBar = (ToolBarView) findViewById(R.id.toolBar);
            mMultipleStatusView = (MultipleStatusView) findViewById(R.id.multipleStatusView);
            mToolBar.setFocusableInTouchMode(true);
            handleToolBar(mToolBar);
            mMultipleStatusView.setContentView(getLayoutId());
            mMultipleStatusView.setOnRetryClickListener(this);
        }

    }

    public abstract P getPresenter();

    @Override
    public boolean setUseLayoutIdCustom() {
        return false;
    }

    @Override
    public void loadData() {
    }

    @Override
    public void reLoadData() {
    }


    public boolean setEventBusEnable() {
        return false;
    }


    @Override
    public void onDestroy() {
        if (setEventBusEnable()) {
            EventBus.getDefault().unregister(this);
        }
        ButterKnife.unbind(this);
//        unSubscribe();
        cancelProgressDialog();
        super.onDestroy();
    }

    @Override
    public MultipleStatusView getMultipleView() {
        return mMultipleStatusView;
    }

    @Override
    public void onRetryClick(MultipleStatusView.ViewStatus viewStatus, View view) {
        loadData();
        reLoadData();
    }

//    @Override
//    public CompositeSubscription getCompositeSubscription() {
//        return mCompositeSubscription;
//    }

//    public void addSubscription(Subscription s) {
//        getCompositeSubscription().add(s);
//    }
//
//    public void unSubscribe() {
//        getCompositeSubscription().unsubscribe();
//    }

    protected void handleToolBar(ToolBarView toolBar) {
        toolBar.setBackPressed(this);
    }

    public void showToast(String content) {
        if (!TextUtils.isEmpty(content)) {
            Toast.makeText(BaseApp.getInstance(), content, Toast.LENGTH_LONG).show();
        }
    }

    public void startProgressDialog(String content) {
        if (loadingDialog == null) {
            loadingDialog = DialogMaker.loadingDialog(this);
        }
        loadingDialog.setCanceledOnTouchOutside(false);
        loadingDialog.show(content);
    }

    public void cancelProgressDialog() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
            loadingDialog = null;
        }
    }

}
